//
//  ShowCouponsController.m
//  PywSdk
//
//  Created by liangyidong on 16/2/26.
//  Copyright © 2016年 zero. All rights reserved.
//

#import "ShowCouponsController.h"
#import "PywMasonry.h"
#import "PywCouponHeadView.h"
#import "PywCouponView.h"
#import "UIImage+Pyw.h"
#import "ApiClientConst.h"

typedef NS_ENUM(NSInteger,CouponType) {
    CouponTypeNotUsed = 1,//未使用
    CouponTypeEnable = 2,//可使用（包含 “未使用”情况）
    CouponTypeUsed = 3,//已使用
    CouponTypeExpire = 4,//已过期
    
};
@interface ShowCouponsController ()<UIScrollViewDelegate>
@property (nonatomic,strong) PywCouponHeadView *headView;
@property (nonatomic,strong) UIScrollView *mainScrollView;
@property (nonatomic,strong) NSArray *couponTypes;
@end

@implementation ShowCouponsController

- (void)dealloc
{
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setUp];
}

#pragma mark - setUp
- (void)setUp
{
    self.title = @"查看代金券";
    [self setLeftBarButtonItem];
    self.navigationItem.rightBarButtonItem = nil;
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    self.couponTypes = @[[NSNumber numberWithInteger:CouponTypeEnable],
                     [NSNumber numberWithInteger:CouponTypeUsed],
                     [NSNumber numberWithInteger:CouponTypeExpire],];
    [self.view addSubview:self.headView];
    [self.view addSubview:self.mainScrollView];
    [self addContrianits];
    [self setUpScrollView];
    
}

- (void)addContrianits
{

    WEAK_SELF;
    [self.headView mas_makeConstraints:^(PywMASConstraintMaker *make) {
        make.left.equalTo(weakSelf.view.mas_left);
        make.top.mas_equalTo(weakSelf.navHeight);
        make.right.equalTo(weakSelf.view.mas_right);
        make.height.mas_equalTo(78);
    }];
    
    [self.mainScrollView mas_makeConstraints:^(PywMASConstraintMaker *make) {
        make.left.right.equalTo(weakSelf.view);
        make.top.equalTo(weakSelf.headView.mas_bottom);
        make.bottom.equalTo(weakSelf.view.mas_bottom);
    }];
}

- (void)setLeftBarButtonItem
{
    UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [backBtn setImage:[[UIImage imageWithName:@"icon_return_normal"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
    [backBtn setImage:[[UIImage imageWithName:@"icon_return_normal"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateHighlighted];
    [backBtn sizeToFit];
    // 设置内容属性
    backBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    backBtn.contentEdgeInsets = UIEdgeInsetsMake(0, -20, 0, 0);
    [backBtn addTarget:self action:@selector(backAction:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backBtn];
    [self.navigationItem.leftBarButtonItem setAccessibilityIdentifier:@"common_back"];
}
/*
 * 重现导航栏，点击“返回”按钮返回用户中心，发送“用户中心”请求刷新通知
 */
- (void)backAction:(UIButton *)sender
{
    [self popView];
    [[NSNotificationCenter defaultCenter] postNotificationName:ReloadUserCenterData object:nil];
}


#pragma mark - getter
- (PywCouponHeadView *)headView
{
    if (!_headView) {
        NSArray *array = @[[NSString stringWithFormat:@"未使用(%@)",@"0"],
                           [NSString stringWithFormat:@"已使用(%@)",@"0"],
                           [NSString stringWithFormat:@"已过期(%@)",@"0"]];
        _headView = [[PywCouponHeadView alloc] initWithItemTitles:array];
        WEAK_SELF;
        _headView.selectItem = ^(NSInteger index) {
            [weakSelf mianScrollViewOffsetWithIndex:index];
        };
    }
    return _headView;
}

- (UIScrollView *)mainScrollView
{
    if (!_mainScrollView) {
        _mainScrollView = [UIScrollView new];
        _mainScrollView.delegate = self;
        _mainScrollView.pagingEnabled = YES;
        _mainScrollView.showsHorizontalScrollIndicator = NO;
    }
    return _mainScrollView;
}
#pragma mark - Actions

- (void)setUpScrollView
{
    __block UIView *lastView = nil;
    for (NSInteger i = 0; i< self.couponTypes.count; i ++) {
        PywCouponView *couponView = [[PywCouponView alloc] initWithStatus:[self.couponTypes[i] integerValue]];
        WEAK_SELF;
        couponView.backTitlesComplete = ^(NSArray *titles) {
            [weakSelf.headView updateItemTitle:titles];
        };
        [self.mainScrollView addSubview:couponView];
        self.mainScrollView.contentSize = CGSizeMake(SCREEN_WIDTH * self.couponTypes.count, 0);
        
        [couponView mas_makeConstraints:^(PywMASConstraintMaker *make) {
            make.top.equalTo(weakSelf.mainScrollView.mas_top);
            make.width.mas_equalTo(weakSelf.mainScrollView.mas_width);
            make.height.equalTo(weakSelf.mainScrollView.mas_height);
            if (lastView) {
                make.left.equalTo(lastView.mas_right);
            }else {
                make.left.equalTo(weakSelf.mainScrollView.mas_left);
            }
        }];
        
        lastView = couponView;
    }
}

- (void)mianScrollViewOffsetWithIndex:(NSInteger)index
{
    [self.mainScrollView setContentOffset:CGPointMake(SCREEN_WIDTH *index, 0) animated:YES];
}


#pragma mark - UIScrollViewDelegate
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    CGFloat x = scrollView.contentOffset.x;
    CGPoint loc = CGPointMake(x, 0);
    NSInteger page = floor(loc.x/self.mainScrollView.bounds.size.width);
    [self.headView switchItemButtonWithIndex:page];
}


#pragma mark - layoutSubviews
/// 竖屏布局
- (void)verticalScreenControlsDistribution
{
    [self updateFrame];
}
/// 横屏布局
- (void)horizontalScreenControlsDistribution
{
    [self updateFrame];
}

- (void)updateFrame
{
    self.mainScrollView.contentSize = CGSizeMake(SCREEN_WIDTH * self.couponTypes.count, 0);
    [self.headView updateItemPadding];
}

@end
